package cn.fql.order.service.impl;

import cn.fql.basic.util.CodeGenerateUtils;
import cn.fql.basic.util.LoginContext;
import cn.fql.basic.util.PageList;
import cn.fql.order.domain.OrderProduct;
import cn.fql.order.domain.OrderProduct;
import cn.fql.order.domain.OrderProduct;
import cn.fql.order.mapper.OrderProductMapper;
import cn.fql.order.query.OrderProductQuery;
import cn.fql.order.service.IOrderProductService;
import cn.fql.basic.service.impl.BaseServiceImpl;
import cn.fql.org.domain.Employee;
import cn.fql.org.domain.Shop;
import cn.fql.org.mapper.EmployeeMapper;
import cn.fql.org.mapper.ShopMapper;
import cn.fql.pet.domain.Pet;
import cn.fql.pet.domain.SearchMasterMsg;
import cn.fql.pet.dto.OrderDto;
import cn.fql.product.domain.Product;
import cn.fql.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class OrderProductServiceImpl extends BaseServiceImpl<OrderProduct> implements IOrderProductService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderProductMapper orderProductMapper;
    @Override
    public PageList<OrderProduct> queryList(OrderProductQuery query, HttpServletRequest request) {
        //1.获取当前登陆人的信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //2.获取管理员Employee的信息
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        //如果是店铺管理员或者店员的话 shop_id就不是空的
        if(employee.getShop_id() != null){
            //设置店铺的id  店铺管理员只能查询出自己店铺的订单
            query.setShopId(employee.getShop_id());
            Shop shop = shopMapper.findById(employee.getShop_id());
            //如果店铺管理员id 和当前登录人的id不一样 那就不是店长
            if(shop.getAdminId() != employee.getId()){
                //店员只能查看自己接单的宠物 不能查询出其他的订单
                query.setEmpId(employee.getId());
            }
        }
        return super.queryPage(query);
    }

    @Override
    public void add(Product product) {
        OrderProduct order = new OrderProduct();
        order.setDigest(product.getName());
        order.setState(0);
        order.setPrice(product.getSaleprice());
        String orderSn = CodeGenerateUtils.generateOrderSn(product.getId());
        order.setOrderSn(orderSn);
        order.setLastPayTime(new Date());
        System.out.println(product);
        orderProductMapper.save(order);
    }

    @Override
    public void confirm(OrderDto orderDto) {
        System.out.println(orderDto);
        OrderProduct orderProduct = new OrderProduct();
        Long id = orderDto.getId();
        orderProductMapper.updatestate(id);
    }


}
